package demo.study;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class WidgetDemo extends Activity implements OnCheckedChangeListener {

	private TextView tv;
	private CheckBox cb1;
	private CheckBox cb2;

	public TextView mTextView1;
	public RadioGroup mRadioGroup1;
	public RadioButton mRadio1, mRadio2;

	private AutoCompleteTextView actv;
	private static final String[] autoStrs = new String[] { "a", "abc", "abcd",
			"abcde", "ba" };
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_widget_demo);

		
		
		tv = (TextView) findViewById(R.id.textview1);
		cb1 = (CheckBox) findViewById(R.id.checkbox1);
		cb2 = (CheckBox) findViewById(R.id.checkbox2);

		cb1.setOnCheckedChangeListener(cbListener);
		cb2.setOnCheckedChangeListener(cbListener);
		

		 /*取得 TextView、RadioGroup、RadioButton对象*/
		mTextView1 = (TextView) findViewById(R.id.textView2);
		mRadioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
		mRadio1 = (RadioButton) findViewById(R.id.radio0);
		mRadio2 = (RadioButton) findViewById(R.id.radio1);
		/*RadioGroup用OnCheckedChangeListener来运行*/ 
		mRadioGroup1.setOnCheckedChangeListener(this);

		// 通过findViewById()方法取到actv
		actv = (AutoCompleteTextView) findViewById(R.id.actv);
		// new ArrayAdapter对象并将autoStr字符串数组传入actv中
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, autoStrs);
		actv.setAdapter(adapter);
		
		
		
		Spinner s = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter<?> adapter2 = ArrayAdapter.createFromResource(
		this, R.array.planets, android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter2);
		
		
		
		
		
		
	
	}

	// 创建菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		// 第一个int类型的group ID参数，代表的是组概念，你可以将几个菜单项归为一组，以便更好的以组的方式管理你的菜单按钮。
		// 第二个int类型的item ID参数，代表的是项目编号。这个参数非常重要，一个itemID对应一个menu中的选项。在后面使用菜单的时候，就靠这个item ID来判断你使用的是哪个选项。
		// 第三个int类型的order ID参数，代表的是菜单项的显示顺序。默认是0，表示菜单的显示顺序就是按照add的显示顺序来显示。
		// 第四个String类型的title参数，表示选项中显示的文字。
		menu.add(0, 0, 0, "关于");
		menu.add(0, 1, 1, "退出");
		return super.onCreateOptionsMenu(menu);
	}

	// 菜单响应
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case 0:
			Toast.makeText(WidgetDemo.this, "欢迎!", Toast.LENGTH_LONG).show();
			break;
		case 1:
			this.finish();
			break;
		}
		return true;
	}


	
	private CheckBox.OnCheckedChangeListener cbListener = new CheckBox.OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			String stv = getString(R.string.hobby);
			String scb1 = getString(R.string.basketball);
			String scb2 = getString(R.string.football);
			// 判断一共有四种情况
			if (cb1.isChecked() == true && cb2.isChecked() == true) {
				tv.setText(stv + scb1 + "," + scb2);
			} else if (cb1.isChecked() == true && cb2.isChecked() == false) {
				tv.setText(stv + scb1);
			} else if (cb1.isChecked() == false && cb2.isChecked() == true) {
				tv.setText(stv + scb2);
			} else {
				tv.setText(stv);
			}
		}
	};

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {

		if (checkedId == WidgetDemo.this.mRadio1.getId()) {
			WidgetDemo.this.mTextView1.setText(WidgetDemo.this.mTextView1.getText()
					.toString() + WidgetDemo.this.mRadio1.getText());
		} else if (checkedId == WidgetDemo.this.mRadio2.getId()) {
			WidgetDemo.this.mTextView1.setText(WidgetDemo.this.mTextView1.getText()
					.toString() + WidgetDemo.this.mRadio2.getText());
		}
	}

}